﻿using DevComponents.DotNetBar;
using Kimd.Common;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI
{
    public partial class FormMultiAxisMoveStep : Office2007Form
    {
        public List<string> PointNames;
        public Step CurrentStep { get; set; }
        public FormMultiAxisMoveStep(Step step, List<string> pointNames)
        {
            InitializeComponent();
            this.CurrentStep = step;
            this.PointNames = pointNames;
            this.EnableGlass = false;
        }

        private void FormMultiAxisMoveStep_Load(object sender, EventArgs e)
        {
            cB_AxisName.Items.Clear();
            cB_AxisName.Items.AddRange(SingletonProvider<MachineResource>.Instance.MachineAxis.Keys.ToArray());
            cB_PointNames.Items.Clear();
            cB_PointNames.Items.AddRange(PointNames.ToArray());

            if((CurrentStep as MultiAxisMoveStep).AxisNames != null)
            {
                foreach (var item in (CurrentStep as MultiAxisMoveStep).AxisNames)
                {
                    lB_Axis.Items.Add(item);
                }
            }
            if(!string.IsNullOrEmpty((CurrentStep as MultiAxisMoveStep).PointName))
            {
                cB_PointNames.SelectedIndex = cB_PointNames.Items.IndexOf((CurrentStep as MultiAxisMoveStep).PointName);
            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (cB_AxisName.SelectedIndex == -1)
                return;
            if (lB_Axis.Items.Contains(cB_AxisName.Text))
                return;
            lB_Axis.Items.Add(cB_AxisName.Text);
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            if (lB_Axis.SelectedIndex == -1)
                return;
            lB_Axis.Items.Remove(lB_Axis.SelectedItem);
        }

        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            if(cB_PointNames.SelectedIndex == -1)
            {
                SystemInfo.ShowToastInfo(this, "选择需要运动到的点位", true);
                return;
            }
            (CurrentStep as MultiAxisMoveStep).AxisNames = new List<string>();
            (CurrentStep as MultiAxisMoveStep).AxisNames.Clear();
            foreach (var item in lB_Axis.Items)
            {
                (CurrentStep as MultiAxisMoveStep).AxisNames.Add(item.ToString());
            }
            (CurrentStep as MultiAxisMoveStep).PointName = cB_PointNames.SelectedItem.ToString();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
